<template>
  <div>
    <!-- <form action="" method="post" enctype="multipart/form-data">
      <input type="file">
      <button type="submit">Submit</button>
    </form> -->
    <LoaderIng v-if="isLoading" text="我已经在拼命加载了"></LoaderIng>
    <GobalHeader :user="currentUser"></GobalHeader>
    <!-- <UploaderFile
      action="/upload"
      :beforeUpload="beforeUpload"
      @file-uploaded="onFileUploaded"
    >
      <h2>点击上传</h2>
      <template #loading>
        <div class="spinner-boder" role="status">
          <span class="sr-only"></span>
        </div>
      </template>

      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" alt="" width="500" />
      </template>
    </UploaderFile> -->
    <router-view></router-view>
    <footer class="text-center py-4 text-seconder bg-light mt-6">
      <small>
        <ul class="list-inline mb-0 flex">
          <li class="list-inline-item">专栏</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">详情</li>
          <li class="list-inline-item">地址</li>
          <li class="list-inline-item">联系</li>
        </ul>
      </small>
    </footer>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, watch } from "vue";
import GobalHeader from "./components/GlobalHeader.vue";
import LoaderIng from "./components/LoaderIng.vue";
import createMessage from "./hooks/createMessage";
import { useStore } from "vuex";
import { GolbalDataProps, ResponseType, ImageProps } from "./store";
import axios from "axios";

export default defineComponent({
  name: "App",
  components: {
    GobalHeader,
    LoaderIng,
  },
  setup() {
    const store = useStore<GolbalDataProps>();
    const currentUser = computed(() => store.state.user);
    const isLoading = computed(() => store.state.loading);
    const token = computed(() => store.state.token);
    const error = computed(() => store.state.error);

    onMounted(() => {
      // isLogin 为 false 头部显示为 登录和注册 就算是 token 值保存到本地了也没有意义
      // isLogin 为 true 头部显示为 你好，XXX 此时 XXX 并没有获取到用户信息，前提是 已经保存了 token
      // 为什么isLogin要取反 它的初始值为 false 在第一次登录之后值为 true 但是刷新的时候就会变回初始值 false

      // if (!currentUser.value.isLogin && token.value) {
      //   axios.defaults.headers.common.Authorization = `Bearer ${token.value}`;
      //   // 再次获取用户信息，使得 XXX 变成登录的用户名
      //   // 为什么如下的方式不可行
      //   // 因为此时要去执行的 login 登录的请求没有获取到 登录所需要的值
      //   // store.dispatch('loginAndFecth')

      //   // 解决方案如下
      //   // const payload = {
      //   //   email: '111@test.com',
      //   //   password: '111111'
      //   // }
      //   // store.dispatch('loginAndFecth', payload)
      //   store.dispatch("fetchCurrentUser");
      // }
      watch(
        () => error.value.status,
        () => {
          const { status, message } = error.value;
          if (status && message) {
            createMessage(message, "error");
          }
        }
      );
    });
    // 验证传入图片格式
    // const beforeUpload = (file: File) => {
    //   console.log(file);
    //   const isJPG = file.type === "image/jpeg";
    //   if (!isJPG) {
    //     createMessage("上传图片只能是 JPG 格式！", "error");
    //   }
    //   return isJPG;
    // };

    // const onFileUploaded = (rawData: ResponseType<ImageProps>) => {
    //   createMessage(`上传图片 ID ${rawData.data._id}`, "success");
    // };

    return {
      currentUser,
      isLoading,
      error,
    };
  },
});
</script>

<style></style>
